<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改调拨单')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-stockAllocation-edit" th:object="${wmsStockAllocation}">
            <input name="allocationId" th:field="*{allocationId}" type="hidden">
            <input name="outStorageId"  th:field="*{outStorageId}" type="hidden" id="outStorageId" >
            <input name="enterStorageId" th:field="*{enterStorageId}" type="hidden" id="enterStorageId" >
            <h4 class="form-header h4">入库信息</h4>
        	<div class="row">            
                <div class="col-sm-6">  
		            <div class="form-group">
		                <label class="col-sm-4 control-label is-required">标题：</label>
		                <div class="col-sm-8">
                    		<input name="title" th:field="*{title}" class="form-control" type="text" required>
	                	</div>   	
		            </div> 
                </div>            	           
                <div class="col-sm-6">            
		             <div class="form-group">    
		                <label class="col-sm-4 control-label is-required">调出仓库：</label>
		                <div class="col-sm-8">
		                	<div class="input-group">
			                    <input name="outStorageName" onclick="selectOutStorage()" id="outStorageName" type="text" placeholder="请选择调出仓库 " th:field="*{outStorageName}" class="form-control" required readonly>
			                	<span class="input-group-addon"><i class="fa fa-search"></i></span>
							</div>
		                </div>
		            </div>  
                </div>
            </div>            
            
        	<div class="row">            
                <div class="col-sm-6">         
		            <div class="form-group">    
		                <label class="col-sm-4 control-label">调拨日期：</label>
		                <div class="col-sm-8">
		                    <div class="input-group date">
		                        <input name="allocationDate" th:value="${#dates.format(wmsStockAllocation.allocationDate, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
		                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
		                    </div>
		                </div>
		            </div>
                </div>            	           
                <div class="col-sm-6">     
		             <div class="form-group">    
		                <label class="col-sm-4 control-label is-required">调入仓库：</label>
		                <div class="col-sm-8">
		                	<div class="input-group">
			                    <input name="enterStorageName" onclick="selectEnterStorage()" id="enterStorageName" type="text" placeholder="请选择调入仓库 " th:field="*{enterStorageName}"  class="form-control" required readonly>
			                	<span class="input-group-addon"><i class="fa fa-search"></i></span>
							</div>
		                </div>
		            </div>      
                </div>
            </div>   
        	<div class="row">            
                <div class="col-sm-6">  
		            <div class="form-group">    
		                <label class="col-sm-4 control-label">负责人：</label>
		                <div class="col-sm-8">                
		                    <div class="input-group">
			                    <input name="allocationBy" onclick="selectUser()" id="allocationBy" th:field="*{allocationBy}" type="text" placeholder="请选择负责人" class="form-control" required readonly>
			                	<span class="input-group-addon"><i class="fa fa-search"></i></span>
							</div>
		                </div>
		            </div>
                </div>  
            </div>     
            
            <h4 class="form-header h4">产品信息</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-success btn-sm" onclick="addColumn()"><i class="fa fa-plus"> 增加</i></button>
                    <button type="button" class="btn btn-danger btn-sm" onclick="sub.delColumn()"><i class="fa fa-minus"> 删除</i></button>
                    <div class="col-sm-12 select-table table-striped">
					    <table id="bootstrap-table"></table>
					</div>
                </div>
            </div>  
        </form>
    </div>
    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
        	<button type="button" class="btn btn-sm btn-primary" onclick="submit()" shiro:hasPermission="crm:stockAllocation:submit"><i class="fa fa-location-arrow"></i>提 交</button>&nbsp;
            <button type="button" class="btn btn-sm btn-success" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "crm/stockAllocation";
        var prefixStock = ctx + "crm/stock";
        var prefixStockProduct = ctx + "crm/stockProduct";
        $("#form-stockAllocation-edit").validate({
            focusCleanup: true
        });
        
        function submit() {        	
       		if ($.validate.form()) {
        			$.modal.confirm("确认提交吗？", function() {
        			var enterStorageId=$('#enterStorageId').val();
                	var outStorageId=$('#outStorageId').val();
                	if(enterStorageId==outStorageId){
                		$.modal.msgWarning("调出仓库和调入仓库不能相同！");
                		return;
                	}       
                	var data = $("#form-stockAllocation-edit").serializeArray();  
                	if(data.length==8){
                		$.modal.msgWarning("请添加产品后再提交!");
                		return;
                	}
                    $.operate.saveTab(prefix + "/submit", data);
               	});
               }
        }

        function submitHandler() {
            if ($.validate.form()) {
            	var enterStorageId=$('#enterStorageId').val();
            	var outStorageId=$('#outStorageId').val();
            	if(enterStorageId==outStorageId){
            		$.modal.msgWarning("调出仓库和调入仓库不能相同！");
            		return;
            	}       
            	var data = $("#form-stockAllocation-edit").serializeArray();  
                $.operate.saveTab(prefix + "/edit", data);
            }
        }

        $("input[name='allocationDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
        
        $(function() {
		    var options = {
			    url: prefixStockProduct + "/list",
			    height: $(window).height()/2,
	            queryParams:{
	            	'opId':[[${allocationId}]],
	            	'type':2
	            },
                pagination: false,
		        showSearch: false,
                showRefresh: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
		        columns: [{
		            checkbox: true
		        },
		        {
		        	field: 'index',
		        	align: 'center',
		        	title: "序号",
                    formatter: function (value, row, index) {
                    	var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    	return columnIndex + $.table.serialNumber(index);
                    }
                },
		        {
		            field: 'productId',
		            align: 'center',
		            title: '产品ID',
		            width:40,
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input class='form-control' type='text' name='products[%s].productId' id='productId_%s' value='%s' readonly required>", index, index, value);
		        		return html;
                    }
		        },
		        {
		            field: 'productName',
		            align: 'center',
		            title: '产品名称',
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input class='form-control' type='text' placeholder='请选择产品'  onclick='selectStorageProductAllocation(" + index + ")' name='products[%s].productName' id='productName_%s' value='%s' required>", index, index, value);
		        		return html;
                    }
		        },
		        {
		            field: 'productNum1',
		            align: 'center',
		            title: '仓库库存数量',
		            width:40,
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input class='form-control' type='number' min=1 name='products[%s].productNum1' id='productNum1_%s' value='%s' readonly required>", index, index, value);
		        		return html;
                    }
		        },
		        {
		            field: 'productNum2',
		            align: 'center',
		            title: '调拨数量',
		            formatter: function(value, row, index) {
		            	var html = $.common.sprintf("<input class='form-control' type='number' min=1 name='products[%s].productNum2' value='%s' required>", index, value);
		        		return html;
                    }
		        }]
		    };
		    $.table.init(options);
		});
        
        function addColumn() {
        	var count = $("#" + table.options.id).bootstrapTable('getData').length;
        	sub.editColumn();
        	
        	$("#" + table.options.id).bootstrapTable('insertRow', {
        		index: count,
                row: {
                	index: $.table.serialNumber(count),
                	productId:"",
                	productName: "",
                	productNum1: "",
                    productNum2: ""
                }
        	});
        }
        
        /*  选择调出仓库 */
        function selectOutStorage() {
        	 var url = ctx + "crm/storage/selectOutStorage";
        	 $.modal.open("选择调出仓库", url);
        }

        /*  选择调入仓库 */
        function selectEnterStorage() {
        	 var url = ctx + "crm/storage/selectEnterStorage";
        	 $.modal.open("选择调入仓库", url);
        }

        /* 选择用户 */
        function selectUser() {
        	var url = ctx + 'system/user/selectUser';
        	$.modal.open("选择人员", url);
        }

        /*  选择产品 */
        function selectProduct() {
        	 var url = ctx + "crm/product/selectProduct";
        	 $.modal.open("选择产品", url);
        }

        /*  选择调拨库存产品 */
        function selectStorageProductAllocation(columnId) {
        	var outStorageId=$('#outStorageId').val();
        	if(outStorageId==''){
        		$.modal.msgWarning("调出仓库不能为空！");
        		return;
        	}
        	var enterStorageId=$('#enterStorageId').val();
        	if(enterStorageId==outStorageId){
        		$.modal.msgWarning("调出仓库和调入仓库不能相同！");
        		return;
        	}
        	 var url = ctx + "crm/stock/selectStorageProductAllocation/"+outStorageId+"/"+columnId;
        	 $.modal.open("选择库存产品", url);
        }
    </script>
</body>
</html>